package org.jeecg.modules.online.desform.util;

import com.alibaba.fastjson.JSONObject;
import com.qiniu.util.Auth;

/**
 * @Description: 七牛云工具类
 * @Author: sunjianlei
 * @Date: 2019-07-11
 * @Version: V2.0
 */
public class QiniuUtils extends QiniuConfig {

    private static Auth auth = null;

    static {
        auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    }

    /**
     * 获取 上传 Token
     *
     * @param bucket 空间名
     */
    public static String getUploadToken(String bucket) {
        return auth.uploadToken(bucket);
    }

    /**
     * 获取 上传 Token
     */
    public static String getUploadToken() {
        return auth.uploadToken(BUCKET);
    }

    /**
     * 获取配置（请勿修改）
     */
    public static JSONObject getConfig() {
        JSONObject json = new JSONObject(4);

        json.put("bucket", BUCKET);

        if (REGION.contains("华东")) {
            json.put("region", "z0");
            json.put("action", "http://upload.qiniup.com/");

        } else if (REGION.contains("华北")) {
            json.put("region", "z1");
            json.put("action", "http://upload-z1.qiniup.com/");

        } else if (REGION.contains("华南")) {
            json.put("region", "z2");
            json.put("action", "http://upload-z2.qiniup.com/");

        } else if (REGION.contains("北美")) {
            json.put("region", "na0");
            json.put("action", "http://upload-na0.qiniup.com/");

        } else if (REGION.contains("东南亚")) {
            json.put("region", "as0");
            json.put("action", "http://upload-as0.qiniup.com/");

        } else {
            json.put("region", "z0");
            json.put("action", "http://upload.qiniup.com/");

        }

        if (DOMAIN.substring(DOMAIN.length() - 1, DOMAIN.length()).equals("/")) {
            json.put("domain", DOMAIN);
        } else {
            json.put("domain", DOMAIN + "/");
        }

        return json;
    }

}
